const MESSAGE_MAP = {
  SUCCESS: '兑换成功',
  EXIST: '先去抽奖再兑换吧',
  NO_ENOUGH: '积分不足',
  COMMON: '系统异常，请稍后再试吧',
};
export default {
  data() {
    return {
      integral: 0,
      luckNum: 0,
      result: [],
      isShowMessage: false,
      messageType: 'success',
      messageText: '',
    };
  },
  methods: {
    refresh() {
      this.getResults();
      this.getCount();
      this.getLuckNumber();
    },
    getResults() {
      this.$http.get('/api/getResults').then(res => {
        this.result = res.data.model;
      });
    },
    getCount() {
      this.$http.get('/api/getCount').then(res => {
        this.integral = res.data.model;
      });
    },
    getLuckNumber() {
      this.$http.get('/api/getLuckNumber').then(res => {
        this.luckNum = res.data.model;
      });
    },
    // 积分兑换
    exchange() {
      this.$http
        .post('/api/exchange')
        .then(res => {
          if (String(res.data.success) !== 'true') {
            this.showMessage(
              'info',
              MESSAGE_MAP[res?.data?.message || 'COMMON']
            );
            return;
          }

          this.showMessage('success', MESSAGE_MAP.SUCCESS);
          this.refresh();
        })
        .catch(() => {
          this.showMessage('error', MESSAGE_MAP.COMMON);
        });
    },
    showMessage(type, message, delay = 1500) {
      this.isShowMessage = true;
      this.messageType = type;
      this.messageText = message;
      setTimeout(() => {
        this.isShowMessage = false;
      }, delay);
    },
  },
  created() {
    this.refresh();
  },
};
